// pages/myshare/myshare.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tempFilePaths:[],
    select: false,
    grade_name: '--请选择--',
    grades: ['拼单', '游戏', '旅行','学习', '拼车', '美食', ],
    obj:{}
  },
  editor:null,
  obj:[],


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let query=wx.createSelectorQuery()
      query.select('#editor').context(res=>{
          this.editor=res.context
      }).exec()
  },
  Undo:function(){
    this.editor.undo()
  },

  Redo:function(){
    this.editor.redo()
  },
  //下拉列表
  bindShowMsg() {
    this.setData({
      select: !this.data.select
    })
  },
  mySelect(e) {
    console.log(e)
    var name = e.currentTarget.dataset.name
    //console.log(name)
    this.setData({
      grade_name: name,
      select: false
    })
},
//上传图片
chooseimage:function(){
  var _this=this;
  wx.chooseImage({
    count:1,
    sizeType:['original','compressed'],
    sourceType:['album','camera'],
    success:function(res){
      //console.log(res)
      _this.setData({
        tempFilePaths:res.tempFilePaths
      })
      //console.log(_this.data.tempFilePaths);
      wx.getFileSystemManager().readFile({
        filePath:_this.data.tempFilePaths[0],//图片本地路径
        encoding:"base64",
        success:res=>{
          _this.setData({
            tempFilePaths:'data:image/jpg;base64,'+res.data
          })
          console.log(res)
        }
      })
    }
  })
},
    

//表单提交
editorPost:function(f){
  //提交成功
  wx.showToast({
    title: '发表成功',
    duration:2000
  })
  //参数
  let {title}=f.detail.value
  this.editor.getContents({
    success:res=>{
        wx.setStorageSync('editorContent', {
            title,
            content:res.text
        })
        var date=new Date()
        var time=date.toLocaleDateString()
        var obj1={
          share_user_id:wx.getStorageSync('user_id'),
          share_title:title,
          share_content:wx.getStorageSync('editorContent').content,
          share_type:this.data.grade_name,
          share_image_src:this.data.tempFilePaths,
          share_time:time,
          share_like:0,
        }
        console.log(obj1)
        
        //console.log(this.data.obj)
        //传数据库
        wx.request({
          url: 'https://api.yidan.work:2004/share',
          header:{'Access-Control-Allow-Origin':'*'},
          header:{'Access-Control-Allow-Methods':'GET,POST'},
          data:obj1,
          method: 'POST',
          success:(res)=>{
            console.log(res);
          }
        }) 
    }  
  })
  



  //跳转
  /*wx.switchTab({
    url:'/pages/share/share',
  })*/
   
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})